package com.b.controller;

import com.b.domain.UserAuth;
import com.b.repository.UserAuthRepository;
import com.b.service.UserAuthService;
import com.b.vo.Result;
import jakarta.annotation.Resource;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.SearchPage;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

/**
 * 用户登录/权限表(user_auth)表控制层
 *
 * @author xxxxx
 */
@RestController
@RequestMapping("/user_auth")
@CrossOrigin
public class UserAuthController {
    /**
     * 服务对象
     */
    @Resource
    private UserAuthService userAuthService;



    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public UserAuth selectOne(Integer id) {
        return userAuthService.selectByPrimaryKey(id);
    }

    //查取用户列表(支持用户名和手机号模糊搜索和id)
    @GetMapping("list")
    public SearchPage<UserAuth> userList(@RequestParam(value = "name", defaultValue = "") String name, @RequestParam(value = "phone", defaultValue = "") String phone, @RequestParam(value = "pageNum", defaultValue = "1") Integer page, @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        PageRequest pageRequest = PageRequest.of(page - 1, pageSize);
        SearchPage<UserAuth> userAuths = userAuthService.findUserAuthByNameLikeAndPhoneLike(pageRequest, name, phone);
        return userAuths;
    }

    //用户封禁(更改数据同时更改es)
    @PostMapping("ban")
    public Result banUser(@RequestParam("userId") Integer userId) {
        try {
            // 调用业务层处理封禁逻辑
            Result result = userAuthService.banUser(userId);
            return result;
        } catch (Exception e) {
            return new Result().setCode("500").setMsg("服务器异常：" + e.getMessage());
        }
    }

}
